#!/bin/sh
# vpn-tun-status from polybar/polybar-scripts
# Modified to show the both the interface name and the IP with different colors.
# Dependencies: jq
# https://github.com/polybar/polybar-scripts/tree/master/polybar-scripts/vpn-tun-status

# Set format options
W='%{F#EEEEEE}'
B='%{F#306BB7}'
# Count the number of tun adapters, e.g. tun0, tun1, etc
TUNTAPS=$(ip -j tuntap list | jq -r '.[].ifname' | grep -c '^tun' )

if [ "$TUNTAPS" != "0" ]; then
    INTERFACE_NAME=$(\
        ip -4 -j addr show \
        | jq '[ .[] | select( .ifname | startswith("tun")) | .ifname ]' \
        | jq -r 'join(", ")'
    )
    INTERFACE_IP=$(\
       ip -4 -j addr show \
        | jq '[ .[] | select( .ifname | startswith("tun")) | .addr_info[0].local ]' \
        | jq -r 'join(", ")'
    )
    echo "$B$INTERFACE_NAME $W$INTERFACE_IP"
else
    MESSAGE_DOWN='no VPN'
    echo "$B$MESSAGE_DOWN"
fi
